// @ts-ignore
import { Effect, Reducer, Subscription, request } from 'umi';

export interface CommentModelState {
  commentList: [];
  total: number;
}

export interface CommentModelType {
  namespace: 'commentModel';
  state: CommentModelState;
  effects: any;
  reducers: any;
  subscriptions: any;
}

const commentModel: CommentModelType = {
  namespace: 'commentModel',

  state: {
    commentList: [],
    total: 0,
  },

  effects: {
    *getListData({ type, payload }: any, { put, call, select }: any) {
      let data = yield request(
        `/comment/${payload.type}?limit=${payload.limit}&offset=${payload.offset}&id=${payload.id}`,
      );
      yield put({
        type: 'saveListData',
        payload: {
          commentList: data.comments,
          total: data.total,
        },
      });
    },
  },
  reducers: {
    saveListData(state: any, action: any) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
  subscriptions: {},
};

export default commentModel;
